package com.uton.rfidcontroller.viewmodel.activity_model;

import android.databinding.ObservableField;

import com.uton.rfidcontroller.common.interfaces.INetController;
import com.uton.rfidcontroller.common.interfaces.IPublicController;
import com.uton.rfidcontroller.common.net.RetrofitFactory;
import com.uton.rfidcontroller.common.utils.LogUtil;
import com.uton.rfidcontroller.common.utils.PwdUtils;
import com.uton.rfidcontroller.common.utils.SharedPreferencesUtils;
import com.uton.rfidcontroller.model.LoginModel;
import com.uton.rfidcontroller.viewmodel.base.BaseViewModel;

import java.util.HashMap;

/**
 * Created by 王家宝 on 2017/9/20.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public abstract class LoginActivityViewModel extends BaseViewModel implements IPublicController.ILoadingController {

    public final ObservableField<String> userName = new ObservableField<>("");
    public final ObservableField<String> userPwd = new ObservableField<>("");

    @Override
    public void refreshData() {

    }

    public void login() {
        // TODO: 2017/9/20 调用登录接口
        showDialog();
        HashMap<String, Object> tempData = new HashMap<>();
        tempData.put("account", userName.get());
//        tempData.put("password", Utils.getMd5Value(userName.get() + userPwd.get()));
        tempData.put("password", userPwd.get());

        LogUtil.d(PwdUtils.checkPwd("123456", "sha1$260149$7463a43d01592f8681e35a43885c4ed37425ee15", "SHA1") + "密码是否一致");
        go(RetrofitFactory.getInstance().login(tempData), new INetController<LoginModel>() {
            @Override
            public void onSuccess(LoginModel response) {
                //保存UserID
                SharedPreferencesUtils.saveUserID(response.getUserId());
                //隐藏Loading
                dismissDialog();
                //登录成功
                loginSuccess();
            }

            @Override
            public void onError() {
                //隐藏Loading
                dismissDialog();
            }

            @Override
            public void onComplete() {
                //隐藏Loading
                dismissDialog();
            }
        });
    }

    public void forgotPwd() {
        goResetPwd();
    }

    public abstract void loginSuccess();

    public abstract void loginFailed();

    public abstract void goResetPwd();


}
